package org.openclinic.service.impl;

import java.util.List;

import org.openclinic.domain.HealthInsurance;
import org.openclinic.exception.DomainException;
import org.openclinic.exception.ErrorCodes;
import org.openclinic.repository.HealthInsuranceRepository;
import org.openclinic.service.HealthInsuranceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

@Service
public class HealthInsuranceServiceImpl implements HealthInsuranceService {
	
	@Autowired
	private HealthInsuranceRepository healthInsuranceRepository;
	
	@Override
	public void delete(long id) {
		healthInsuranceRepository.delete(id);
	}
	
	@Override
	public HealthInsurance find(long id) {
		return healthInsuranceRepository.findOne(id);
	}
	
	@Override
	public List<HealthInsurance> list() {
		return healthInsuranceRepository.findAllOrderByName();
	}
	
	@Override
	public HealthInsurance save(HealthInsurance healthInsurance) {
		try {
			return healthInsuranceRepository.save(healthInsurance);
		} catch (DataIntegrityViolationException e) {
			throw new DomainException("healthInsurance", "name", ErrorCodes.UNIQUE_VIOLATED, healthInsurance.getName());
		}
	}
}
